import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:lib_core/lib_core.dart';
import 'package:lib_furnace/lib_furnace.dart';

/// Author: Shper
/// Created: 2023-08-10
class GTFurnace {

  static final GTFurnace _instance = GTFurnace._();

  static GTFurnace get instance => _instance;

  // 消息通道
  final BasicMessageChannel _navigationMessageChannel = const BasicMessageChannel("com.gateio.flutter.navigation", StringCodec());

  static BasicMessageChannel get navigationMessageChannel => _instance._navigationMessageChannel;

  // 路由工厂需要 Main 中初始化
  late GTFurnaceRouteFactory _routeFactory;

  GTFurnaceRouteFactory get routeFactory => _routeFactory;

  GTFurnace._() {
  }

  static void setRouteFactory(GTFurnaceRouteFactory routeFactory) {
    _instance._routeFactory = routeFactoryWrapper(routeFactory);
  }

  // 禁用原生侧滑返回，只针对 iOS 平台
  void interactivePopDisabled() {
    /// 通知原生是否可以开启侧滑返回手势
    /// 当canPop()为 true 时说明导航处于二级或多级界面，此时原生需要禁止原生的侧滑返回
    /// 当canPop()为 false 时说明导航处于一级界面，此时原生需要打开原生的侧滑返回
    var message = <String, dynamic>{};
    message["flutterPageCanPop"] = true;
    GTFurnace.navigationMessageChannel.send(json.encode(message));
  }

  // 禁用原生侧滑返回，只针对 iOS 平台
  void interactivePopEnable() {
    /// 通知原生是否可以开启侧滑返回手势
    /// 当canPop()为 true 时说明导航处于二级或多级界面，此时原生需要禁止原生的侧滑返回
    /// 当canPop()为 false 时说明导航处于一级界面，此时原生需要打开原生的侧滑返回
    var message = <String, dynamic>{};
    message["flutterPageCanPop"] = false;
    GTFurnace.navigationMessageChannel.send(json.encode(message));
  }

}
